$(document).ready(function () {
    var projectId = $("#projectId").val();
	var path= contextPath+"/project/getReporter";
	var params = {"projectId":projectId};
	$.ajax({
        type:"POST",
        url:path,
        data:JSON.stringify(params),
        contentType:"application/json; charset=utf-8",
        dataType: "json",
        async:false,
        success:function(respone){
        	if(respone.code=="0"){
        		var mydata = [];
        		var projectName = respone.project.projectName;
        		$.each(respone.tClasses,function(index,tClass){
        			var json = {
		                    id: tClass.classId,
		                    projectName: projectName,
		                    deviceId: tClass.deviceId,
		                    deviceName: tClass.deviceName,
		                    successRate: (tClass.successRate*100)+"%",
		                    createTime: tClass.createTime,
		                    handle: "<a href='#' onclick='reportDetail("+projectId+","+tClass.classId+","+tClass.totalCase+",\""+projectName+"\");'>查看详情</a>"
		                };
        			mydata.push(json);
        		});
        		 // Configuration for jqGrid Example 1
                $("#table_list_1").jqGrid({
                    data: mydata,
                    datatype: "local",
                    height: 250,
                    autowidth: true,
                    shrinkToFit: true,
                    rowNum: 14,
                    rowList: [10, 20, 30],
                    colNames: ['序号', '项目名称','模块名称','设备号','设备名称','日期', '详细'],
                    colModel: [
                        {
                            name: 'id',
                            index: 'id',
                            width: 60,
                            sorttype: "int"
                        },
                        {
                            name: 'projectName',
                            index: 'projectName',
                            width: 90,
                            sorttype: "string"
                        },
                        {
                        	name: 'moduleName',
                            index: 'moduleName',
                            width: 80
                        },
                        {
                            name: 'deviceId',
                            index: 'deviceId',
                            width: 90,
                            sorttype: "string"
                        },
                        {
                            name: 'deviceName',
                            index: 'deviceName',
                            width: 90,
                            sorttype: "string"
                        },
                        {
                            name: 'createTime',
                            index: 'createTime',
                            width: 80,
                            align: "right",
                            sorttype: "string"
                        },
                        {
                            name: 'handle',
                            index: 'handle',
                            width: 80,
                            align: "right",
                            sorttype: "string"
                        }
                    ],
                    pager: "#pager_list_1",
                    viewrecords: true,
                    caption: "报告列表",
                    hidegrid: false
                });           
                // Add responsive to jqGrid
                $(window).bind('resize', function () {
                    var width = $('.jqGrid_wrapper').width();
                    $('#table_list_1').setGridWidth(width);
                });
        	}else{
        		alert("加载测试报告失败");
        	}
        },error:function(respone){
            alert("请求发生错误:"+respone);
        }
    });

});

function reportDetail(projectId,classId,totalCase,projectName){
	var path= contextPath+"/project/reportDetail";
	var params = {"projectId":projectId,"classId":classId};
	$.ajax({
        type:"POST",
        url:path,
        data:JSON.stringify(params),
        contentType:"application/json; charset=utf-8",
        dataType: "json",
        async:false,
        success:function(respone){
        	if(respone){
        		var html="";
        		/*html=html+"<div class='col-lg-10' style='padding-left:330px;'><div><table class='table'><tbody><tr>";
        		html=html+"<td><button type='button' class='btn btn-danger m-r-sm'>S</button></td>";
        		html=html+"<td><button type='button' class='btn btn-primary m-r-sm'>SUITES</button>"+totalCase+"</td>";
        		html=html+"<td><button type='button' class='btn btn-info m-r-sm'>TESTS</button>3</td></tr>";
        		html=html+"<tr><td><button type='button' class='btn btn-info m-r-sm'>通过</button>4</td>";
        		html=html+"<td><button type='button' class='btn btn-success m-r-sm'>失败</button>5</td>";
        		html=html+"<td><button type='button' class='btn btn-danger m-r-sm'>PENDING</button>6</td></tr>";
        		html=html+"</tbody></table></div></div>";*/
        		html=html+"<a href='#' onclick='queryReporter("+projectId+",\""+projectName+"\")'><i class='fa fa-sign-out'></i>返回</a>";
        		html=html+"<div class='col-lg-12'><div class='ibox float-e-margins'><div class='ibox-title'>";
        		html=html+"<h5>测试报告</h5></div><div class='ibox-content'><table class='table table-striped'>";
        		html=html+"<thead><tr><th>结果</th><th>方法名</th><th>步骤数</th><th>成功数</th><th>失败数</th><th>成功率</th><th>时间</th><th>详情</th></tr>";
        		html=html+"</thead><tbody>";
        		
        		$.each(respone.tCases,function(index,tCase){
        			if(tCase.caseErrors==0){
        				html=html+"<tr><td><i class='fa fa-check' style='color:#16987e;'></td>";
        			}else{
        				html=html+"<tr><td><i class='fa fa-times' style='color:#ed5565;'></i></td>";
        			}
        			html=html+"<td><span class='line'>"+tCase.caseName+"</span></td>";
            		html=html+"<td><span class='line'>"+tCase.stepCount+"</span></td>";
            		html=html+"<td><span class='line'>"+tCase.caseErrors+"</span></td>";
            		html=html+"<td style='color:#d43f3a'>"+tCase.caseFailures+"</td>";
            		html=html+"<td><span class='line'>"+(tCase.successRate*100)+"%"+"</span></td>";
            		html=html +"<td>"+tCase.spendTime+"</td><td class='text-navy' onclick='step.index.getStepInfo("+tCase.caseId+","+projectId+","+classId+","+totalCase+",\""+projectName+"\");' >";
            		html=html +"<a href='#' class='btn btn-white btn-sm'><i class='fa fa-folder'></i> 查看 </a></td></tr>";
        		});
        		html = html+"</tbody></table></div></div></div>";
        		$("#uiContent").html(html);
        	}else{
        		alert("加载测试报告失败");
        	}
        },error:function(respone){
            alert("请求发生错误:"+respone);
        }
    });
}
//获取测试报告
function queryReporter(projectId,projectName){
	window.location.href = contextPath + "/project/repindex?projectId="+projectId;
}
/*function totalProject(){
		
		var path = contextPath + "/project/totalProject";
		var params = {
			"currentPage":1,
			"pageSize":10
		};
		$.ajax({
			type:"POST",
	        url:path,
	        data:JSON.stringify(params),
	        contentType:"application/json; charset=utf-8",
	        dataType: "html",
	        async:false,
	        success:function(response){
	        	if(response){
	        		if(response.indexOf("registerLabel")>0){
	        			$("#main_body").html(response);
	        		}else{
	        			$("#main_content").html(response);
	        		}
        			var options = {
        	                size:"large",
        	                bootstrapMajorVersion:3,
        	                currentPage: 1,
        	                numberOfPages: 1,
        	                totalPages:1
        	            };
    	            var element = $('#bp-3-element-lg');
    	            element.bootstrapPaginator(options);
	        	}
	        },error:function(response){
	            alert("请求发生错误:"+response);
	        }
		});
}*/